// pages/studyPlanAdd/studyPlanAdd.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    labelList: [], 
    tempList: [],
    restList: [],
    showRest: false,
    publisherList: [], // 出版社列表
    paperList: [], // 试卷列表
    selectedLabel: {},
    selectedPublisherId: 0,
    pageNo: 1,
    pageTotal: 1,
    title: '',
    cheight: 0,
    columnId: 'paper_0'
  },
  bindChange_select: function (ev) {
    // 定义一个变量curindex 储存触发事件的数组对象的下标  
    const curindex = ev.target.dataset.current
    //根据下标 改变该数组对象中的index值  
    this.data.objArray[curindex].index = ev.detail.value
    //把改变某个数组对象index值之后的全新objArray重新 赋值给objArray  
    this.setData({
      objArray: this.data.objArray
    })
  },
  bindscrolltolower () {
    if (this.data.pageNo <= this.data.pageTotal) {
      this.getPlist()
    }
  },
  showMore () {
    //显示更多标签
    this.setData({
      showRest: !this.data.showRest
    })
  },
  // 获取出版社列表
  getPublishList() {
    app.requestModule('/app/course/getCourseSource', {}, this.getPublishListCallback, () => { }, 'GET')
  },
  getPublishListCallback (res) {
    if (res.data) {
      this.setData({
        publisherList: res.data
      })
      this.getPlist()
    }
  },
  //获取标签列表
  getLabelList() {
    app.requestModule('/app/course/getCourseType', {}, this.getLabelListCallback, () => { }, 'GET')
  },
  getLabelListCallback (res) {
    if (res.data) {
      let templist = [], restlist = []
      let list = res.data
      list.map((v, k) => {
        if (k < 7) {
          templist.push(v)
        }
        else {
          restlist.push(v)
        }
      })
      this.setData({
        labelList: list,
        tempList: templist,
        restList: restlist
      })
      // 默认选中第一个获取试卷
      this.setData({
        selectedLabel: list[0]
      })
      this.getPublishList()
    }
  },
  // 获取试卷列表
  getPlist() {
    wx.showLoading({
      title: '加载中',
    })
    let params = { typeId: this.data.selectedLabel.key, publisherId: this.data.publisherList[this.data.selectedPublisherId], pageNo: this.data.pageNo, pageSize: 6}
    app.requestModule('/app/course/getCourseLists2', params, this.getPlistCallback, () => { }, 'GET')
  },
  getPlistCallback (res) {
    wx.hideLoading()
    if(res.data) {
      let tlist = [] //这里是课程里面的类型数组
      let paperList = res.data
      paperList.map((value, key) => {
        value.selectedPulishId = 0
        value.sourseDetails.map((v, k) => {
          let newlist = []
          v.courseList.filter((v1, k1) => {
            let len = Math.ceil(v.courseList.length / 3)
            for (let i = 0; i < len; i++) {
              newlist[i] = v.courseList.slice(i*3, (i+1) * 3)
            }
          })
          v.courseList = newlist
        })
      })
     // console.log(paperList)
      this.setData({
        paperList: paperList
      })
    }
  },
  getPaperList (e) {
    // 获取点击的标签
    var item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index
    console.log(e.currentTarget.dataset)
    if (this.data.selectedLabel.key !== item.key) {
      this.setData({
        selectedLabel: item,
        columnId: 'paper_' + index
      })
      console.log(this.data.columnId)
    }
  },
  bindPickerChange (e) {
    let key = e.detail.value //key的值
    let labelId = e.currentTarget.dataset.lableid
      // 根据拿到的数组来更新
    let list = this.data.paperList
    list.map((v, k) => {
        if (v.typeId === labelId) {
          list[k].selectedPulishId = key
          if (v.sourseDetails) {
            v.sourseDetails.map((v1, k1) => {
              console.log(v1.sourse, this.data.publisherList[key])
              let a = v.sourseDetails
              if (v1.sourse == this.data.publisherList[key]) {
                v.sourseDetails.splice(k1, 1)
                v.sourseDetails.splice(0, 0, v1)
              }
            })
          }
        }
      })
      this.setData({
        paperList: list
      })
  },
  addToPlan (e) {
    // 添加到计划
    let item = e.currentTarget.dataset.item
    wx.showModal({
      title: '提示',
      content: '确定要添加并前去设定该学习计划吗',
      success: (res) => {
        if (res.confirm) {
          wx.setStorageSync('courseplan', JSON.stringify(item))
          app.formDirectUrl('/pages/studyPlan/studyPlan')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let isNew = options.isNew
    this.setData({
      title: isNew ? '创建学习计划' : '增加学习计划'
    })
    this.getLabelList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})